<?php
/**
 * 前台登录 退出操作
 *
 *
 *
 *
 
 */

// use Shopnc\Tpl;
defined('InShopNC') or exit('Access Invalid!');

class loginControl extends mobileHomeControl
{

    public function __construct()
    {
        parent::__construct();
    }
    
 

    /**
     * 登录
     */
    public function indexOp()
    {
        $array = array();
        //从WiFi平台接数据
        if($_POST['client']=='web'){
            if (empty($_POST['username']) || ! in_array($_POST['client'], $this->client_type_array)) {
                output_error('登录失败');
            }
        }else{
            if (empty($_POST['username']) || empty($_POST['password']) || ! in_array($_POST['client'], $this->client_type_array)) {
                output_error('登录失败');
            }
            $array['member_passwd'] = $_POST['password'];
        }
        
        
        $model_member = Model('member');

        $array['member_name'] = $_POST['username'];
        // $array['member_passwd'] = md5($_POST['password']);
        
        $member_info = $model_member->getMemberInfo($array);
        if (! empty($member_info)) {
            $data_login=array();
            //推荐有奖，查找是否是第一次登录和是否有推荐人
            if ($member_info['is_first_logined']==1){
                if($member_info['share_member_id']!=null && !empty($member_info['share_member_id'])){
                    //得到当前用户是被推荐人，在查看推荐人是否符合获得代金券条件
                    //去活动表查看是否过期
                    $model_lottery_activity=Model('lottery_activity');
                    $condition['lottery_activity_id']=999;
                    $activity_info=$model_lottery_activity->getPureActivityInfo($condition);
                    if($activity_info['exchange_end_date']>TIMESTAMP){
                        //推荐人可以领取代金券
                        //查找礼品表
                        $model_lottery_gift=Model('lottery_gift');
                        $gift_con['lottery_activity_id']=$condition['lottery_activity_id'];
                        $gift_info=$model_lottery_gift->getGiftInfo($gift_con);
                        $voucher_id=31;
                        if(!empty($gift_info) && is_array($gift_info) && !empty($gift_info['goods_id'])){
                            $voucher_id=$gift_info['goods_id'];
                        }
                        $member_id=$member_info['share_member_id'];
                        $this->_voucher_get($member_id,$voucher_id);
                        
                    }
                }
            }
            
            
            //当前登录时间
            //当前登陆的前一次时间
            //登陆次数
            $data_login=array(
                'member_login_time'=>time(),
                'member_old_login_time'=>$member_info['member_login_time'],
                'member_login_num'=>array('exp'=>'member_login_num+1'),
                'member_login_ip'=>$_SERVER['REMOTE_ADDR'],
                'is_first_logined'=>0
            );
            $model_member->editMember($array, $data_login);
            
            if ($member_info['mobile_attribution'] == null) {
                $content = $this->get_mobileArea($member_info['member_mobile']);
                preg_match_all("/(\w+):'([^']+)/", $content, $m);
                $mobile = array_combine($m[1], $m[2]);
                $data['mobile_attribution'] = $mobile['carrier'];
               
                $data['mobile_province']=$mobile['province'];
                
                if($member_info['member_client'] == null){
                    $data['member_client']=$_POST['client'];
                }
                // 写入
                $result = $model_member->editMember($array, $data);
            }
            $token = $this->_get_token($member_info['member_id'], $member_info['member_name'], $_POST['client']);
            if ($token) {
                output_data(array(
                    'username' => $member_info['member_name'],
                    'client' => $_POST['client'],
                    'key' => $token
                ));
            } else {
                output_error('登录失败');
            }
        } else {
            output_error('用户名密码错误');
        }
    }

    /**
     * 登录生成token
     */
    private function _get_token($member_id, $member_name, $client)
    {
        $model_mb_user_token = Model('mb_user_token');
        
        // 重新登录后以前的令牌失效
        // 暂时停用
        // $condition = array();
        // $condition['member_id'] = $member_id;
        // $condition['client_type'] = $_POST['client'];
        // $model_mb_user_token->delMbUserToken($condition);
        
        // 生成新的token
        $mb_user_token_info = array();
        $token = md5($member_name . strval(TIMESTAMP) . strval(rand(0, 999999)));
        $mb_user_token_info['member_id'] = $member_id;
        $mb_user_token_info['member_name'] = $member_name;
        $mb_user_token_info['token'] = $token;
        $mb_user_token_info['login_time'] = TIMESTAMP;
        $mb_user_token_info['client_type'] = $_POST['client'];
        
        $result = $model_mb_user_token->addMbUserToken($mb_user_token_info);
        
        if ($result) {
            return $token;
        } else {
            return null;
        }
    }

    /**
     * 注册
     */
    public function registerOp()
    {
        
        $model_member = Model('member');
        $register_info = array();
        $register_info['username'] = $_POST['username'];
        $register_info['password'] = $_POST['password'];
        $register_info['password_confirm'] = $_POST['password_confirm'];
        // $register_info['email'] = $_POST['email'];
        $register_info['mobile'] = $_POST['mobile'];
        $register_info['member_client'] = $_POST['client'];
        
        // 查找手机归属地
        $content = $this->get_mobileArea($register_info['mobile']);
        preg_match_all("/(\w+):'([^']+)/", $content, $m);
        $mobile = array_combine($m[1], $m[2]);
        $register_info['mobile_attribution'] = $mobile['carrier'];
        $register_info['mobile_province']=$mobile['province'];
        $member_info = $model_member->register($register_info);
        if (! isset($member_info['error'])) {
            $token = $this->_get_token($member_info['member_id'], $member_info['member_name'], $_POST['client']);
            if ($token) {
                output_data(array(
                    'username' => $member_info['member_name'],
                    'key' => $token
                ));
            } else {
                output_error('注册失败');
            }
        } else {
            output_error($member_info['error']);
        }
    }
    
    public function shareRuleOp(){
        //查找activity
        $condition['lottery_activity_id']=999;
        $model_activity_lottery=Model('lottery_activity');
        $activity_info=$model_activity_lottery->getPureActivityInfo($condition);
        //查找注册代金券
        $model_voucher=Model('voucher');
        $condition_vou['voucher_t_id'] = array('IN','13,14,15,16');
        $voucher_list=$model_voucher->getVoucherTemplateList($condition_vou);
        $amount=0;//代金券总金额
        foreach ($voucher_list as $key =>$val){
            $amount +=$val['voucher_t_price'];
        }
        
        output_data(array('voucher_amount'=>$amount,'rule'=>$activity_info['comments']));
    }
    
    /**
     * testRegister
     */
    public function shareRegisterOp(){
        $model_member = Model('member');
        
        //获得验证码
        $obj_cache = Cache::getInstance('file');
        $valcode = $obj_cache->get(md5($_POST['username']));
        $valcode=explode('-', $valcode);
        if($_POST['valcode'] !=$valcode[0] || $valcode[1] !='reg'){
            output_error("验证码不正确");
        }
        
        
        $register_info = array();
        $register_info['username'] = $_POST['username'];
        $password = $this->_randstr();
        //判断是否通过推荐有奖注册
        if($_REQUEST['share_id']){
            //给被推荐人加一个推荐人id
            $model_recommend=Model('recommend');
            $recom_con['share_id']=$_REQUEST['share_id'];
            $recommend_info=$model_recommend->getOneRecommend($recom_con);
            $register_info['share_member_id']=$recommend_info['share_member_id'];
            $register_info['is_first_logined']=1;

        }
        
        //设置密码
        $register_info['password'] = md5(md5($password) + $register_info['username']);
        $register_info['password_confirm'] = $register_info['password'];
        $register_info['mobile'] = $register_info['username'];
        
        $register_info['member_client'] = $_POST['client']?$_POST['client']:'wap';
        
        // 查找手机归属地
        $content = $this->get_mobileArea($register_info['mobile']);
        preg_match_all("/(\w+):'([^']+)/", $content, $m);
        $mobile = array_combine($m[1], $m[2]);
        $register_info['mobile_attribution'] = $mobile['carrier'];
        $register_info['mobile_province']=$mobile['province'];
        
        $member_info = $model_member->register($register_info);
        if (! isset($member_info['error'])) {
            $token = $this->_get_token($member_info['member_id'], $member_info['member_name'], $_POST['client']);
            $member_id=$member_info['member_id'];
            $this->_voucher_get($member_id, 13);
            $this->_voucher_get($member_id, 14);
            $this->_voucher_get($member_id, 15);
            $this->_voucher_get($member_id, 16);
            
            $tpl_id = '1803736';//模板
            $this->_sendTempPassSms($register_info['username'], $password, $tpl_id);
            $obj_cache->rm(md5($_POST['username']));
            
   
            output_data(array(
                'username' => $member_info['member_name'],
            ));
        } else {
            output_error(array('username'=>$_POST['username']));
        }
    }

    /**
     * 抓取手机信息
     */
    function get_mobileArea($mobile)
    {
        $url = "http://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel=" . $mobile . "&t=" . time();
        $content = file_get_contents($url);
        return mb_convert_encoding($content, 'utf-8', 'GBK,UTF-8,ASCII');
    }

    /**
     * 抓取网页
     * 
     * @param unknown $url            
     */
    public function get_mobile_area($mobile)
    {
        $url = "http://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel=" . $mobile;
        // 初始化
        $ch = curl_init();
        // 设置选项，包括URL
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        // 函数中加入下面这条语句
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        
        // 执行并获取HTML文档内容
        $output = curl_exec($ch);
        if (curl_errno($ch)) {
            print curl_error($ch);
        }
        $output = mb_convert_encoding($output, 'utf-8', 'GBK,UTF-8,ASCII');
        
        // 释放curl句柄
        curl_close($ch);
        // 打印获得的数据
        return ($output);
    }

    /**
     * web注册
     */
    public function webregisterOp()
    {
        $model_member = Model('member');
        
        $register_info = array();
        $register_info['username'] = $_POST['username'];
        // 对password进行加密处理
        $output = md5(md5($_POST['password']) + $_POST['username']);
        $register_info['password'] = $output;
        $register_info['password_confirm'] = $output;
        $register_info['member_client'] = $_POST['client'];
        $register_info['mobile'] = $_POST['username'];
        // 查找手机归属地
        $content = $this->get_mobileArea($register_info['mobile']);
        preg_match_all("/(\w+):'([^']+)/", $content, $m);
        $mobile = array_combine($m[1], $m[2]);
        $register_info['mobile_attribution'] = $mobile['carrier'];
        $register_info['mobile_province']=$mobile['province'];
        $member_info = $model_member->register($register_info);
        if (! isset($member_info['error'])) {
            $token = $this->_get_token($member_info['member_id'], $member_info['member_name'], $_POST['client']);
            if ($token) {
                output_data(array(
                    'username' => $member_info['member_name'],
                    'key' => $token
                ));
            } else {
                output_error('注册失败');
            }
        } else {
            output_error($member_info['error']);
        }
    }
    
    /**
     * wap注册
     */
    public function wapRegisterOp()
    {
        $model_member = Model('member');
        
        //获得验证码
        $obj_cache = Cache::getInstance('file');
        $valcode = $obj_cache->get(md5($_POST['username']));
        $valcode=explode('-', $valcode);
        if($_POST['valcode'] !=$valcode[0] || $valcode[1] !='reg'){
            output_error("验证码不正确");
        }else{
            $obj_cache->rm(md5($_POST['username']));
        }
        $register_info = array();
        $register_info['username'] = $_POST['username'];
        // 对password进行加密处理
        //$output = md5(md5($_POST['password']) + $_POST['username']);
        $register_info['password'] = $_POST['password'];
        $register_info['password_confirm'] = $_POST['password'];
        // $register_info['email'] = $_POST['email'];
        $register_info['mobile'] = $_POST['username'];
        $member_info = $model_member->register($register_info);
        if (! isset($member_info['error'])) {
            $member_id=$member_info['member_id'];
            $this->_voucher_get($member_id, 13);
            $this->_voucher_get($member_id, 14);
            $this->_voucher_get($member_id, 15);
            $this->_voucher_get($member_id, 16);
            
            $token = $this->_get_token($member_info['member_id'], $member_info['member_name'], $_POST['client']);
            if ($token) {
                //调用WiFi平台
                $userCode = $_POST['username'];
                $mac = $_POST['client'];
                $data = array('userCode' => $userCode,'mac'=>$mac);
                $post_data = json_encode($data);
                $url = 'http://120.76.77.72/open/regist';
                $ch = curl_init();
                curl_setopt($ch, CURLOPT_URL, $url);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
                curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
                curl_setopt($ch, CURLOPT_POST, 1); // 启用POST提交
                curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
                $result = json_decode(curl_exec($ch));

                output_data(array(
                    'username' => $member_info['member_name'],
                    'key' => $token
                ));
            } else {
                output_error('注册失败');
            }
        } else {
            output_error($member_info['error']);
        } 
        
        /* $obj_cache = Cache::getInstance('file');
        $key = $_POST['key'];
        $valcode = $obj_cache->get($key);
        if($valcode == $_POST['valcode']) {
            output_data(array(
                'msg' => '成功    1:'.$valcode.'  2:'.$_POST['valcode']
            ));
        } else {
            output_error('失败    1:'.$valcode.'  2:'.$_POST['valcode']);
        } */
        
        
    }

    /**
     * web注册测试
     */
    public function webregistertestOp()
    {
        $model_member = Model('member');
        
        $register_info = array();
        $register_info['username'] = $_GET['username'];
        // 对password进行加密处理
        $output = md5(md5($_GET['password']) + $_GET['username']);
        $register_info['password'] = $output;
        $register_info['password_confirm'] = $output;
        // $register_info['email'] = $_POST['email'];
        $register_info['mobile'] = $_GET['username'];
        $member_info = $model_member->register($register_info);
        if (! isset($member_info['error'])) {
            $token = $this->_get_token($member_info['member_id'], $member_info['member_name'], $_GET['client']);
            if ($token) {
                output_data(array(
                    'username' => $member_info['member_name'],
                    'key' => $token
                ));
            } else {
                output_error('注册失败');
            }
        } else {
            output_error($member_info['error']);
        }
    }

    public function smscodeOp($mobile = '')
    {
        
        // if (!empty($_POST['mobile'])) {
        // $tpl_id = '1192361';
        // $this->_sendSms($_POST['mobile'],$tpl_id);
        // }else{
        // output_error('请输入手机号码！');
        // }
        if (! empty($_POST['mobile'])) {
            $mobile = $_POST['mobile'];
            $model_member = Model('member');
            $array = array();
            $array['member_mobile'] = $mobile;
            $member_info = $model_member->getMemberInfo($array);
            if (! empty($member_info)) {
                output_error('该手机号码已经注册！');
            } else {
                $tpl_id = '1192361';
                $this->_sendSms($mobile, $tpl_id);
            }
        } else {
            output_error('请输入手机号码！');
        }
    }

    public function wapSmsCodeOp($mobile = '')
    {
        if (! empty($_POST['mobile'])) {
            $mobile = $_POST['mobile'];
            $model_member = Model('member');
            $array = array();
            $array['member_mobile'] = $mobile;
            $member_info = $model_member->getMemberInfo($array);
            if (! empty($member_info)) {
                output_error('该手机号码已经注册！');
            } else {
                $tpl_id = '1192361';
                 $this->_sendSms($mobile,$tpl_id,'reg');
               /*  $code = $this->_randstr(4);
                $obj_cache = Cache::getInstance('file');
                //设置 redis 字符串数据
                $obj_cache->set(md5($code), $code);
                output_data(array(
                    'msg' => '短信发送成功！1:'.$code.'.2:'.$obj_cache->get(md5($code)),
                    'key' => md5($code)
                )); */
            }
        } else {
            output_error('请输入手机号码！');
        }
    }

    public function getSmsCodeOp($mobile = '')
    {
        if (! empty($_POST['mobile'])) {
            $mobile = $_POST['mobile'];
            $model_member = Model('member');
            $array = array();
            $array['member_mobile'] = $mobile;
            $member_info = $model_member->getMemberInfo($array);
            if (! empty($member_info)) {
                output_error('该手机号码已经注册！');
            } else {
                $tpl_id = '1192361';
                // $this->_sendSms($mobile,$tpl_id);
                $code = $this->_randstr(4);
                //$_SESSION['valcode'] = $code;
                output_data(array(
                    'msg' => '短信发送成功！'
                ));
            }
        } else {
            output_error('请输入手机号码！');
        }
    }

    /**
     * 产生一个指定长度的随机字符串,并返回给用户
     * 
     * @access public
     * @param int $len
     *            产生字符串的位数
     * @return string
     */
    private function _randstr($len = 6)
    {
        $chars = '0123456789';
        mt_srand((double) microtime() * 1000000 * getmypid());
        $password = '';
        while (strlen($password) < $len)
            $password .= substr($chars, (mt_rand() % strlen($chars)), 1);
        return $password;
    }

    /**
     * 发送临时密码
     * @param unknown $mobile
     * @param unknown $tpl_id
     * @param string $smstype
     */
    private function _sendTempPassSms($mobile, $code, $tpl_id,$smstype='')
    {
        $sms = new Sms();
        $content = ('#password#') . '=' . urlencode($code);
        $result = $sms->mytpl_send_sms($tpl_id, $mobile, $content);
        if ($result) {
            return $code;
        } else {
            return 123456;
        }
    }
    
    
    private function _sendSms($mobile, $tpl_id,$smstype='')
    {
        $code = $this->_randstr(4);
        $obj_cache = Cache::getInstance('file');
        //设置 redis 字符串数据
        if($smstype==''){
            $obj_cache->set(md5($mobile), $code);
        }else{
            $obj_cache->set(md5($mobile), $code.'-'.$smstype);
        }
        
        //$obj_cache->set('mobile', $mobile);
        //$valcode = $obj_cache->get('code');
        $valcode = $obj_cache->get(md5($mobile));
        $valcode=explode('-', $valcode);
        $sms = new Sms();
        $content = ('#code#') . '=' . urlencode($code);
        $result = $sms->mytpl_send_sms($tpl_id, $mobile, $content);
        if ($result) {
            output_data(array(
                'code'=>$valcode[0],
                'msg' => '短信发送成功！'
            ));
        } else {
            output_error('短信发送失败，请检测手机号码格式！');
        }
    }

    /**
     * 头像上传
     */
    public function avatar_uploadOp()
    {
        import('function.thumb');
        $member_id = '2';
        // 上传图片
        $upload = new UploadFile();
        $upload->set('thumb_width', 500);
        $upload->set('thumb_height', 499);
        $ext = strtolower(pathinfo($_FILES['avatar']['name'], PATHINFO_EXTENSION));
        $upload->set('file_name', "avatar_$member_id.$ext");
        $upload->set('thumb_ext', '_new');
        $upload->set('ifremove', true);
        $upload->set('default_dir', ATTACH_AVATAR);
        if (! empty($_FILES['pic']['tmp_name'])) {
            $result = $upload->upfile('pic');
            if (! $result) {
                output_error($upload->error);
            }
        } else {
            output_error('上传失败，请尝试更换图片格式或小图片');
        }
    }

    /**
     * 验证手机号是否注册
     */
    public function checkMobileOp()
    {
        if (! empty($_POST['mobile'])) {
            $mobile = $_POST['mobile'];
            $model_member = Model('member');
            $array = array();
            $array['member_mobile'] = $mobile;
            $member_info = $model_member->getMemberInfo($array);
            if (! empty($member_info)) {
                $tpl_id = '1192361';
                $this->_sendSms($mobile, $tpl_id,'check');
                
            } else {
                output_error('该手机号码尚未注册！');
            }
        } else {
            output_error('请输入手机号码！');
        }
    }

    /**
     * 重置密码
     */
    public function reSetPassWordOp()
    {
        if (! empty($_POST['mobile'])) {
            if (! empty($_POST['password'])) {
                if (! empty($_POST['password_confirm'])) {
                    $mobile = $_POST['mobile'];
                    $password = $_POST['password'];
                    $model_member = Model('member');
                    $array = array();
                    $array['member_mobile'] = $mobile;
                    $data = $model_member->getMemberInfo($array);
                    if (! empty($data)) {
                        $data['member_passwd'] = trim($password);
                        if ($model_member->editMember($array, $data)) {
                            output_data(array(
                                'msg' => '操作成功！'
                            ));
                        } else {
                            output_error('操作失败');
                        }
                    } else {
                        output_error('该手机号码尚未注册！');
                    }
                } else {
                    output_error('请输入确认的密码！');
                }
            } else {
                output_error('请输入新的密码！');
            }
        } else {
            output_error('请输入手机号码！');
        }
    }
    
    /**
     * 重置密码
     */
    public function wapResetPassWordOp()
    {
        //获得验证码
        $obj_cache = Cache::getInstance('file');
        $valcode = $obj_cache->get(md5($_POST['mobile']));
        $valcode=explode('-', $valcode);
        //$sendmobile = $obj_cache->get('mobile');
    

        if($_POST['valcode'] !=$valcode[0] || $valcode[1]!='check'){
            output_error("验证码不正确");
        }else{
            $obj_cache->rm(md5($_POST['mobile']));
        }
        if ($_POST['mobile'] != null && $_POST['mobile'] != "") {
            if ($_POST['password'] != null && $_POST['password'] != "") {
                if ($_POST['password_confirm'] != null && $_POST['password_confirm'] != "") {
                    if($_POST['password'] != $_POST['password_confirm']){
                        output_error("确认密码不正确");
                    }
                    $mobile = $_POST['mobile'];
                    $password = $_POST['password'];
                    $model_member = Model('member');
                    $array = array();
                    $array['member_mobile'] = $mobile;
                    $data = $model_member->getMemberInfo($array);
                    if (! empty($data)) {
                        $data['member_passwd'] = trim($password);
                        if ($model_member->editMember($array, $data)) {
                            output_data(array(
                                'msg' => '操作成功！'
                            ));
                        } else {
                            output_error('操作失败');
                        }
                    } else {
                        output_error('该手机号码尚未注册！');
                    }
                } else {
                    output_error('请输入确认的密码！');
                }
            } else {
                output_error('请输入新的密码！');
            }
        } else {
            output_error('请输入手机号码！');
        }
    }
    
    
    //领取代金券
    private function _voucher_get($member_id,$voucher_id){
        $model_voucher=Model('voucher');
        //$member_id=$this->member_info['member_id'];
        //$voucher_id=intval($_REQUEST ['voucher_id']);
        //$use_point=$_REQUEST ['use_point'];
        $condition['voucher_t_id']=$voucher_id;
        $voucher_info=$model_voucher->getVoucherTemplateList($condition);
    
        $check=$model_voucher->getCanChangeTemplateInfo($voucher_id,$member_id,0,0);//查询是否可以兑换
        if($check['state']){
            //可以领取
            if($use_point==1){
                $getVoucher=$model_voucher->exchangeVoucher($voucher_info[0],$member_id);//兑换代金券
                if($getVoucher['state']){
                    output_data($getVoucher);
                }
                else{
                    output_error($getVoucher['msg']);
                }
            }
            else{
                //查询会员信息
                if (!$member_name){
                    $member_info = Model('member')->getMemberInfoByID($member_id);
                    $member_name = $member_info['member_name'];
                }
                //添加代金券信息
                $insert_arr = array();
                $insert_arr['voucher_code'] = $model_voucher->get_voucher_code();
                $insert_arr['voucher_t_id'] = $voucher_info[0]['voucher_t_id'];
                $insert_arr['voucher_title'] = $voucher_info[0]['voucher_t_title'];
                $insert_arr['voucher_desc'] = $voucher_info[0]['voucher_t_desc'];
                $insert_arr['voucher_type'] = $voucher_info[0]['voucher_t_type'];
    
                //新增处理代金券有效期
                if ($voucher_info[0]['voucher_t_datetype'] == 1) {
                    $insert_arr['voucher_start_date'] = $voucher_info[0]['voucher_t_start_date'];
                    $insert_arr['voucher_end_date'] = $voucher_info[0]['voucher_t_end_date'];
                } else if ($voucher_info[0]['voucher_t_datetype'] == 2) {
                    $insert_arr['voucher_start_date'] = time();
                    $insert_arr['voucher_end_date'] = $insert_arr['voucher_start_date']+$voucher_info[0]['voucher_t_cycle']*3600*24;
                }
                $insert_arr['voucher_price'] = $voucher_info[0]['voucher_t_price'];
                $insert_arr['voucher_limit'] = $voucher_info[0]['voucher_t_limit'];
                $insert_arr['voucher_store_id'] = $voucher_info[0]['voucher_t_store_id'];
                $insert_arr['voucher_state'] = 1;
                $insert_arr['voucher_active_date'] = time();
                $insert_arr['voucher_owner_id'] = $member_id;
                $insert_arr['voucher_owner_name'] = $member_name;
    
                $result = $model_voucher->table('voucher')->insert($insert_arr);
                if ($result){
                    //代金券模板的兑换数增加
                    $model_voucher->editVoucherTemplate(array('voucher_t_id'=>$template_info['voucher_t_id']), array('voucher_t_giveout'=>array('exp','voucher_t_giveout+1')));
                    wcache($member_id, array('voucher_count' => array('exp','voucher_count+1')), 'm_voucher');
                    return true;
                    //output_data(array('state'=>true,'msg'=>'领取成功'));
                } else {
                    return false;
                    //output_data(array('state'=>false,'msg'=>'领取失败'));
    
                }
            }
             
        }
        else{
            output_error($check['msg']);
        }
    
    }
    
}
